package com.robot.config;

import com.robot.model.FireAlarm;
import com.robot.model.Maintenance;
import com.robot.mapper.FireAlarmMapper;
import com.robot.mapper.MaintenanceMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 数据初始化器
 * 注意：当使用MySQL时，数据初始化通过src/main/resources/data.sql完成
 * 此类仅在dev和test环境下使用，用于开发和测试
 */
@Component
@Profile({"dev", "test"})  // 仅在开发和测试环境使用
public class DataInitializer implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(DataInitializer.class);

    @Autowired
    private FireAlarmMapper fireAlarmMapper;
    
    @Autowired
    private MaintenanceMapper maintenanceMapper;
    
    @Override
    public void run(String... args) {
        logger.info("开始初始化测试数据...");
        
        // 检查数据库中是否已有数据
        if (fireAlarmMapper.selectCount(null) > 0 || maintenanceMapper.selectCount(null) > 0) {
            logger.info("数据库中已存在数据，跳过初始化");
            return;
        }
        
        // 初始化一些测试数据
        initFireAlarmData();
        initMaintenanceData();
        
        logger.info("测试数据初始化完成");
    }
    
    private void initFireAlarmData() {
        // 一分公司数据
        FireAlarm alarm1 = new FireAlarm();
        alarm1.setCompany("一分公司");
        alarm1.setYear(2023);
        alarm1.setMonth(5);
        alarm1.setCount(3);
        alarm1.setCreatedAt(LocalDateTime.now());
        fireAlarmMapper.insert(alarm1);
        
        FireAlarm alarm2 = new FireAlarm();
        alarm2.setCompany("一分公司");
        alarm2.setYear(2023);
        alarm2.setMonth(6);
        alarm2.setCount(1);
        alarm2.setCreatedAt(LocalDateTime.now());
        fireAlarmMapper.insert(alarm2);
        
        // 二分公司数据
        FireAlarm alarm3 = new FireAlarm();
        alarm3.setCompany("二分公司");
        alarm3.setYear(2023);
        alarm3.setMonth(5);
        alarm3.setCount(2);
        alarm3.setCreatedAt(LocalDateTime.now());
        fireAlarmMapper.insert(alarm3);
        
        FireAlarm alarm4 = new FireAlarm();
        alarm4.setCompany("二分公司");
        alarm4.setYear(2023);
        alarm4.setMonth(12);
        alarm4.setCount(0);
        alarm4.setCreatedAt(LocalDateTime.now());
        fireAlarmMapper.insert(alarm4);
    }
    
    private void initMaintenanceData() {
        // 一分公司数据
        Maintenance maintenance1 = new Maintenance();
        maintenance1.setCompany("一分公司");
        maintenance1.setYear(2023);
        maintenance1.setMonth(5);
        maintenance1.setStatus("已完成");
        maintenance1.setDeviceCount(100);
        maintenance1.setCompletedCount(95);
        maintenance1.setCreatedAt(LocalDateTime.now());
        maintenanceMapper.insert(maintenance1);
        
        // 二分公司数据
        Maintenance maintenance2 = new Maintenance();
        maintenance2.setCompany("二分公司");
        maintenance2.setYear(2023);
        maintenance2.setMonth(12);
        maintenance2.setStatus("进行中");
        maintenance2.setDeviceCount(150);
        maintenance2.setCompletedCount(80);
        maintenance2.setCreatedAt(LocalDateTime.now());
        maintenanceMapper.insert(maintenance2);
    }
} 